import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:moose_app/core/model/poetry_model.dart';
import 'package:moose_app/ui/values/values.dart';

import '../home_item.dart';

class ATHRankingContent extends StatefulWidget {
// with AutomaticKeepAliveClientMixin

  @override
  _ATHRankingContentState createState() => _ATHRankingContentState();
}

class _ATHRankingContentState extends State<ATHRankingContent>
    with AutomaticKeepAliveClientMixin {
  EasyRefreshController _easyRefreshController;

  // 条目总数
  int _count = 2;

  @override
  void initState() {
    super.initState();
    _easyRefreshController = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      margin: kCommonMarin,
      child: EasyRefresh.custom(
        enableControlFinishRefresh: true,
        enableControlFinishLoad: true,
        controller: _easyRefreshController,
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2), () {
            _easyRefreshController.finishRefresh(success: true);
          });
        },
        onLoad: () async {
          await Future.delayed(Duration(seconds: 2), () {
            setState(() {
              _count += 2;
            });
            _easyRefreshController.finishLoad(noMore: _count >= 10);
          });
        },
        slivers: [_buildRankingListView()],
      ),
    );
  }

  Widget _buildRankingListView() {
    return SliverList(
      delegate: SliverChildBuilderDelegate((context, index) {
        return ATHHomeItem(
          poetryItemModel: PoetryItemModel(
              author: "李白",
              title: "蜀道难",
              body:
                  "噫吁嚱，危乎高哉！蜀道之难，难于上青天！\n蚕丛及鱼凫，开国何茫然！尔来四万八千岁，不与秦塞通人烟。西当太白有鸟道，可以横绝峨眉巅。地崩山摧壮士死，然后天梯石栈相钩连。\n上有六龙回日之高标，下有冲波逆折之回川。黄鹤之飞尚不得过，猿猱欲度愁攀援。青泥何盘盘，百步九折萦岩峦。扪参历井仰胁息，以手抚膺坐长叹。\n 问君西游何时还？畏途巉岩不可攀。但见悲鸟号古木，雄飞雌从绕林间。又闻子规啼夜月，愁空山。蜀道之难，难于上青天，使人听此凋朱颜。\n连峰去天不盈尺，枯松倒挂倚绝壁。飞湍瀑流争喧豗，砯崖转石万壑雷。其险也如此，嗟尔远道之人胡为乎来哉！\n剑阁峥嵘而崔嵬，一夫当关，万夫莫开。所守或匪亲，化为狼与豺。朝避猛虎，夕避长蛇；磨牙吮血，杀人如麻。锦城虽云乐，不如早还家。蜀道之难，难于上青天，侧身西望长咨嗟。"),
        );
      }, childCount: _count),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
